const db = require('../DBcofig/DBConnection')
module.exports = {
    getMedicalPlan(req, res) {
        let { typeName, packageName } = req.query;
        // console.log(req.query);
        let sql = 'select * FROM packagetype AS a,medicalplan AS b,physicalexaminationproject AS c,xmtc AS d  WHERE  a.typeId=b.type_id AND b.medicalID=d.medical_id AND c.physicalExaminationID=d.pep_id AND C.projectStatus="true" AND a.typeStatus="true"'
        let arr = []
        if (typeName != undefined && typeName != '') {
            sql += ` and typeName = ?`
            arr.push(typeName)
        }
        if (packageName != undefined && packageName != '') {
            sql += ` and packageName like ?`
            arr.push("%" + packageName + "%")
        }
        // console.log(sql, arr);
        db.dbconfig(res, sql, arr, function(data) {
            // console.log(data, "啦啦啦");
            if (data != null) {
                res.send({
                    code: 200,
                    msg: '查询成功',
                    data: data
                });
            } else {
                res.send({
                    code: 404,
                    msg: '查询失败',
                });
            }

        })

    },
    addMedicalPlan(req, res) {
        let {
            packageID,
            packageName,
            packageImage,
            typeName,
            projectName,
            packagePrice,
            packageIntroduce,
            announcements,
            packageStatus
        } = req.body
        console.log(req.body, '前端数据');
        let sql = 'insert into medicalPlan values (null,?,?,?,?,?,?,?,?,?)'
        let arr = [
                packageID,
                packageName,
                packageImage,
                typeName,
                projectName,
                packagePrice,
                packageIntroduce,
                announcements,
                packageStatus
            ]
            // console.log(arr);
        db.dbconfig(res, sql, arr, function(data) {
            console.log(data, "啦啦啦");
            // res.send({ code: 200 })
            if (data.affectedRows >= 1) {
                res.send({
                    code: 200,
                    msg: '增加成功',
                });
            } else {
                res.send({
                    code: 404,
                    msg: '增加失败',
                });
            }

        })
    },
    delMedicalPlan(req, res) {
        let { id } = req.body;
        console.log(id);
        let sql = 'delete from medicalplan where medicalId=?';
        let arr = [id];
        db.dbconfig(res, sql, arr, function(data) {
            // console.log(data, "啦啦啦");
            if (data.affectedRows >= 1) {
                res.send({
                    code: 200,
                    msg: '删除成功',
                });
            } else {
                res.send({
                    code: 404,
                    msg: '删除失败',
                });
            }
        })
    },
    getMedicalName(req, res) {
        let { id } = req.query;
        let sql = 'select * FROM medicalplan'
        let arr = []
        if (id != undefined) {
            sql += ` where type_id= ?`
            arr.push(parseInt(id))
        }
        db.dbconfig(res, sql, arr, function(data) {
            // console.log(data, "啦啦啦");
            if (data != null) {
                res.send({
                    code: 200,
                    msg: '查询成功',
                    data: data
                });
            } else {
                res.send({
                    code: 404,
                    msg: '查询失败',
                });
            }

        })

    },
    delAllMedicalPlan(req, res) {
        let id = req.body;
        // console.log(req.body, 'eee');
        let arr = [];
        let str = '';
        for (let item in Object(id)) {
            console.log(item);
            let b = id[item]
            str += '?' + ','
            let c = []
            c.push(b)
            arr.push(c)

        }
        let str1 = '(' + str.substr(0, str.length - 1) + ")"
        let sql = 'delete from medicalplan where medicalId in';
        sql += str1
            // console.log(arr, sql);
        db.dbconfig(res, sql, arr, function(data) {
            console.log(data, "啦啦啦");
            if (data.affectedRows >= 1) {
                res.send({
                    code: 200,
                    msg: '删除成功',
                });
            } else {
                res.send({
                    code: 404,
                    msg: '删除失败',
                });
            }
        })

    },
}